<template>
    <div id="login">
        <div class="title">喷码机设备管理系统</div>
        <div class="loginBox">
            <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="100px" >
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginForm.username" clearable placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" type="password" clearable placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="loginFormStart">登陆</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</template>

<script>
    export default {
        name: "login",

        data(){
            return{
                loginForm:{
                    username:'admin',
                    password:'12345678',
                },
                loginFormRules:{
                    username:[
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 5, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
                    ],
                    password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                    ],
                }
            }
        },
        methods:{
            loginFormStart(){
                this.$refs.loginFormRef.validate(async valid=>{
                    if(valid){
                        const params=new URLSearchParams();
                        params.append("username",this.loginForm.username);
                        params.append("password",this.loginForm.password);
                        const {data:res}=await this.$http.post("login",params);
                        if(res.code){
                            window.sessionStorage.setItem('token',res.result.token);
                            this.$router.push('home');
                            this.$message.success("登陆成功")
                        }else {
                            this.$message.error("登陆失败")
                        }
                    }else{
                        this.$message.error("输入格式不合法");
                        return;
                    }
                })
            }
        }
    }
</script>

<style  scoped>
    #login{
        height: 100vh;
        width: 100%;
        background: url("../assets/bgLogin.jpg") no-repeat 50%;
        background-size: 100% 100%;
    }

    .loginBox{
        width: 450px;
        height: 250px;
        border: 1px solid #cccccc;
        border-radius:5px;
        background: white;
       position: absolute;
        top: 50%;
        left: 50%;
        /*box-sizing: border-box;*/
        padding-top: 50px;
        padding-right: 50px;
        transform: translate(-50%,-50%);
        box-shadow: 5px 5px 5px #cccccc;
    }
    .title{
        position: absolute;
        top: 25%;
        left: 50%;
        transform: translate(-50%,-50%);
        color: white;
        font-size: 26px;
        font-weight: 700;

    }
    .el-button{
        width: 100%;
    }

</style>
